package com.china08.yxyapi.api.schwebsite.app;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.ThumborUtil;

@CrossOrigin
@RestController("SwsIndexAppAllArticleController")
public class SwsIndexAppAllArticleController extends YxyApiBaseController {

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsArticleRepository swsArticleRepository;

    @RequestMapping(value = "/api_yw/schwebsite/app/all/articles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");
        String schoolId = byDomainName.getSchoolId();

        Pageable pageable = reqModel.getPageable(Direction.DESC, "showDate");

        Page<SwsArticle> articles = swsArticleRepository.findByOwnerSchoolIdAndAuditStatus(schoolId, 1, pageable);
        List<RespModel> list = new ArrayList<RespModel>();

        Assert.isTrue(null != articles, "暂无文章");
        for (SwsArticle swsArticle : articles) {
            RespModel model = new RespModel();
            model.id = swsArticle.getId();
            model.title = Jsoup.parse(swsArticle.getTitle()).text();
            model.showDate = DateToolUtils.format(swsArticle.getPublishDate(), DateToolUtils.FORMAT_SHORT_YMD);
            model.content = Jsoup.parse(swsArticle.getContent()).text();
            model.author = swsArticle.getAuthor();
            if (StringUtils.isNotBlank(swsArticle.getFaceImg())) {
                model.faceImg_180_124 = ThumborUtil.schoolWebSiteResize(swsArticle.getFaceImg(), 180, 124);
                model.faceImg_140_88 = ThumborUtil.schoolWebSiteResize(swsArticle.getFaceImg(), 140, 88);
            } else {
                model.faceImg_180_124 = "";
                model.faceImg_140_88 = "";
            }

            if (StringUtils.isBlank(swsArticle.getFaceImg())) {
                // 获取详情中的第一张图片作为推荐图片
                Document document = Jsoup.parse(swsArticle.getContent());
                Elements elements = document.select("img");
                if (elements.size() > 0) {
                    String attr = elements.get(0).attr("src");
                    model.faceImg_180_124 = ThumborUtil.schoolWebSiteResize(attr, 180, 124);
                    model.faceImg_140_88 = ThumborUtil.schoolWebSiteResize(attr, 140, 88);
                }

            }

            if (StringUtils.isBlank(swsArticle.getSection2Id())) {
                model.section2Id = "0";
            } else {
                model.section2Id = swsArticle.getSection2Id();
            }
            model.section1Id = swsArticle.getSection1Id();
            list.add(model);
        }

        Page<RespModel> pageRes = new PageImpl<RespModel>(list, pageable, articles.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String title;

        public String showDate;

        public String section1Id;

        public String section2Id;

        public String content;

        public String faceImg_180_124;

        public String faceImg_140_88;

        public String author;
    }
}
